import I18n, {
  changeLanguage,
  languageKey,
  resetLanguage,
} from './../config/i18n';
/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow strict-local
 */

import React, {useState, useEffect} from 'react';
import {
  Button,
  SafeAreaView,
  ScrollView,
  StatusBar,
  StyleSheet,
  Text,
  useColorScheme,
  View,
} from 'react-native';
import {
  Colors,
  DebugInstructions,
  Header,
  LearnMoreLinks,
  ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
import AsyncStorage from '@react-native-community/async-storage';

const Section = ({children, title}) => {
  const isDarkMode = useColorScheme() === 'dark';
  return (
    <View style={styles.sectionContainer}>
      <Text
        style={[
          styles.sectionTitle,
          {
            color: isDarkMode ? Colors.white : Colors.black,
          },
        ]}>
        {title}
      </Text>
      <Text
        style={[
          styles.sectionDescription,
          {
            color: isDarkMode ? Colors.light : Colors.dark,
          },
        ]}>
        {children}
      </Text>
    </View>
  );
};

const App = () => {
  const isDarkMode = useColorScheme() === 'dark';
  const backgroundStyle = {
    backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
  };

  const [language, setLanguage] = useState();
  useEffect(() => {
    getLanguage();
  }, []);

  const getLanguage = async () => {
    const res = await AsyncStorage.getItem(languageKey);
    setLanguage(res ? res : I18n.t('跟随系统'));
  };

  return (
    <SafeAreaView style={backgroundStyle}>
      <StatusBar barStyle={isDarkMode ? 'light-content' : 'dark-content'} />
      <ScrollView
        contentInsetAdjustmentBehavior="automatic"
        style={backgroundStyle}>
        <Header />
        <View>
          <Text>
            {I18n.t('当前语言为：')}
            {language}
          </Text>
          <Button
            title={I18n.t('点击切换语言')}
            onPress={() => changeLanguage(language === 'zh' ? 'en' : 'zh')}
          />
          <Button title={I18n.t('恢复默认')} onPress={() => resetLanguage()} />
        </View>
        <View
          style={{
            backgroundColor: isDarkMode ? Colors.black : Colors.white,
          }}>
          <Section title={I18n.t('第一步')}>
            {I18n.t('编辑')}
            <Text style={styles.highlight}>App.js</Text>{' '}
            {I18n.t('然后回来看修改的内容。')}
          </Section>
          <Section title={I18n.t('刷新')}>
            <ReloadInstructions />
          </Section>
          <Section title={I18n.t('调试')}>
            <DebugInstructions />
          </Section>
          <Section title={I18n.t('更多')}>
            {I18n.t('阅读文档查看更多内容:')}
          </Section>
          <LearnMoreLinks />
        </View>
      </ScrollView>
    </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  sectionContainer: {
    marginTop: 32,
    paddingHorizontal: 24,
  },
  sectionTitle: {
    fontSize: 24,
    fontWeight: '600',
  },
  sectionDescription: {
    marginTop: 8,
    fontSize: 18,
    fontWeight: '400',
  },
  highlight: {
    fontWeight: '700',
  },
});
export default App;
